package com.tencent.sr.iris.activity.service.event;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityEventTypeEnum;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityTypeEnum;
import com.tencent.sr.iris.activity.service.config.TaskAndActExecutorConfig;
import com.tencent.sr.iris.activity.service.event.activity.IrisActivityExecutor;
import com.tencent.sr.iris.activity.service.event.handler.IrisEventHandler;
import com.tencent.sr.iris.activity.service.event.task.IrisTaskExecutor;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 事件、活动、任务策略
 *
 * @author xiaojun
 */
@Component
public class EventExecutorContext implements InitializingBean {


    @Resource
    private TaskAndActExecutorConfig taskExecutorConfig;

    /**
     * 事件处理器集合 key:事件类型，value:事件handler
     */
    @Autowired
    private Map<String, IrisEventHandler> irisEventHandlerMap;

    /**
     * 活动处理器 key:活动bizId，value:活动executor
     */
    @Autowired
    private Map<String, IrisActivityExecutor> irisActivityExecutorMap;

    /**
     * 任务处理器 key:任务bizId，value:任务executor
     */
    @Autowired
    private Map<String, IrisTaskExecutor> irisTaskExecutorMap;


    @Override
    public void afterPropertiesSet() throws Exception {

    }

    public static void main(String[] args) {
        Map<String, String> map = Maps.newHashMap();
        map.put("MONTH_FIRST_TASK", "irisCommissionTaskExecutor");
        map.put("MONTH_SECOND_TASK", "irisCommissionTaskExecutor");
        map.put("INVITE_TASK", "irisInviteCommissionTaskExecutor");
        map.put("REGIST_TASK", "userBecomeTraineeTaskExecutor");
        map.put("MEMBER", "userLevelUpPromoTaskExecutor");
        map.put("SENIOR", "userLevelUpPromoTaskExecutor");
        map.put("SUPER", "userLevelUpPromoTaskExecutor");
        map.put("FOSTER", "userLevelUpFosterTaskExecutor");
        System.out.println(JSON.toJSONString(map));

        Map<String, String> mapAct = Maps.newHashMap();
        mapAct.put("MONTH_GIFT", "irisMonthGiftActivityExecutor");
        mapAct.put("LEVEL_UP_FOSTER_GIFT", "userLevelUpFosterActivityExecutor");
        mapAct.put("REGIST_GIFT", "userBecomeTraineeActivityExecutor");
        mapAct.put("LEVEL_UP_PROMO_GIFT", "userLevelUpPromoActivityExecutor");
        mapAct.put("INVITE_GIFT", "irisInviteGiftActivityExecutor");

        System.out.println(JSON.toJSONString(mapAct));
    }

    /**
     * 根据事件类型获取事件处理器
     *
     * @param eventType
     * @return
     */
    public IrisEventHandler getEventHandler(ActivityEventTypeEnum eventType) {
        IrisEventHandler irisEventHandler = irisEventHandlerMap.get(eventType.getCode());
        return irisEventHandler;
    }

    /**
     * 根据活动bizId获取活动处理器
     *
     * @param actBizId
     * @return
     */
    public IrisActivityExecutor getActivityHandler(String actBizId) {
        String activityExecutorName = taskExecutorConfig.getActivityExecutorMap().get(actBizId);
        IrisActivityExecutor activityExecutor = irisActivityExecutorMap.get(activityExecutorName);
        return activityExecutor;
    }

    /**
     * 根据任务bizId获取任务处理器
     *
     * @param taskBizId
     * @return
     */
    public IrisTaskExecutor getTaskHandler(String taskBizId) {
        String taskExecutorName = taskExecutorConfig.getTaskExecutorMap().get(taskBizId);
        IrisTaskExecutor irisTaskExecutor = irisTaskExecutorMap.get(taskExecutorName);
        return irisTaskExecutor;
    }
}
